import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:get/get.dart';

class FormValidators {
  /// 邮箱-帐号校验
  static String? Function(String?) emailValidator =
      FormBuilderValidators.compose([
    FormBuilderValidators.required(
      errorText: 'validation_required'.trParams({'field': 'username'.tr}),
    ),
    FormBuilderValidators.match(
      r'^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$',
      errorText: 'validation_email'.tr,
    ),
  ]);

  /// 密码校验（6-18 位）
  static String? Function(String?) passwordValidator =
      FormBuilderValidators.compose([
    FormBuilderValidators.required(
      errorText: 'validation_required'.trParams({'field': 'password'.tr}),
    ),
    FormBuilderValidators.minLength(
      8,
      errorText: 'validation_min_length'.trParams({'count': '8'}),
    ),
    FormBuilderValidators.match(
      r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$',
      errorText: 'validation_password'.tr,
    ),
  ]);

  /// 必填字段校验
  static String? Function(String?) requiredValidator(String fieldName) {
    return FormBuilderValidators.required(
      errorText: 'validation_required'.trParams({'field': fieldName}),
    );
  }
}
